'use strict'
// 初始化数据
let arr = [
    {
        id: 1,
        name: 'admin',
        pwd: '123456'
    },
    {
        id: 2,
        name: '张二狗',
        pwd: 'xcvasd'
    },
    {
        id: 3,
        name: '李五',
        pwd: 'uilgh'
    },
    {
        id: 4,
        name: '李三',
        pwd: 'zxcqweasd'
    },
    {
        id: 5,
        name: '张五',
        pwd: 'ghfgfh'
    },
    {
        id: 6,
        name: '张三',
        pwd: 'asd123'
    },
]

let fn_user_get = async (ctx, next) => {
    ctx.body = arr
}

let fn_user_post = async (ctx, next) => {
    let id = ctx.request.body.id
    if (id === '' || id === null || id === undefined) {
        let username = ctx.request.body.username;
        let userpwd = ctx.request.body.userpwd;
        let list = arr.filter(item => {
            return item.name === username && item.pwd === userpwd;
        })

        if (list.length > 0) {
            ctx.body = {
                code: 200,
                data: list[0],
                msg: '登录成功！'
            }
        } else {
            ctx.body = {
                code: 404,
                data: '',
                msg: '用户名或密码错误！请重试'
            }
        }
    } else {
        let newusername = ctx.request.body.name;
        let newuserpwd = ctx.request.body.pwd;
        let obj = {
            id: arr[arr.length - 1].id + 1,
            name: newusername,
            pwd: newuserpwd
        }
        arr.push(obj)
        ctx.body = ''
    }
}

// let fn_user_post_value = async (ctx, next) => {
//     let newusername = ctx.request.body.name;

//     if (newusername === '' || newusername === null || newusername === undefined) {
//         fn_user_post_delete(ctx, next);
//     } else {
//         fn_user_post_update(ctx, next);
//     }
// }

// let fn_user_post_update = async (ctx, next) => {
//     let id = ctx.request.body.id
//     let newusername = ctx.request.body.name;
//     let newuserpwd = ctx.request.body.pwd;

//     for (let i = 0; i < arr.length; i++) {
//         if (arr[i].id == id) {
//             arr[i].name = newusername;
//             arr[i].pwd = newuserpwd;
//         }
//     }
//     ctx.body = ''
// }

// let fn_user_post_delete = async (ctx, next) => {
//     let id = ctx.request.body.id
//     for (let i = 0; i < arr.length; i++) {
//         if (arr[i].id == id) {
//             arr.splice(i, 1)
//         }
//     }
//     ctx.body = arr;
// }



let fn_user_put = async (ctx, next) => {
    let id = ctx.request.body.id
    let newusername = ctx.request.body.name;
    let newuserpwd = ctx.request.body.pwd;

    for (let i = 0; i < arr.length; i++) {
        if (arr[i].id == id) {
            arr[i].name = newusername;
            arr[i].pwd = newuserpwd;
        }
    }
    ctx.body = ''
}

let fn_user_delete = async (ctx, next) => {
    let id = ctx.request.body.id
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].id == id) {
            arr.splice(i, 1)
        }
    }
    ctx.body = arr;
}

module.exports = {
    'get /user': fn_user_get,
    'post /user': fn_user_post,
    'put /user/:id': fn_user_put,
    'delete /user/:id': fn_user_delete,
};